package com.ruoyi.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.security.convert.SecurityConvert;
import com.ruoyi.security.domain.Security;
import com.ruoyi.security.dto.SecurityDto;
import com.ruoyi.security.dto.SyncSecurityListDto;
import com.ruoyi.security.enums.SecurityNodeEnum;
import com.ruoyi.security.enums.SecurityTypeEnum;
import com.ruoyi.security.enums.StockBoardCodePrefixEnum;
import com.ruoyi.security.mapper.SecurityMapper;
import com.ruoyi.security.response.SecurityResponse;
import com.ruoyi.security.service.craw.ICrawSecurityService;
import com.ruoyi.security.service.ISecurityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yeoman
 * @date 2025/4/30 17:12
 * @description
 */
@Slf4j
@Service
public class SecurityServiceImpl implements ISecurityService {

    @Resource
    private SecurityMapper securityMapper;

    @Resource
    private ICrawSecurityService crawService;

    @Override
    public List<Security> list(SecurityDto dto) {
        LambdaQueryWrapper<Security> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getSecuritySymbol())) {
            queryWrapper.eq(Security::getSymbol, dto.getSecuritySymbol());
        }
        if (StringUtils.isNotBlank(dto.getSecurityName())) {
            queryWrapper.like(Security::getName, dto.getSecurityName());
        }
        if (StringUtils.isNotBlank(dto.getSecurityType())) {
            queryWrapper.eq(Security::getType, dto.getSecurityType());
        }
        if (StringUtils.isNotBlank(dto.getSecurityExchange())) {
            queryWrapper.eq(Security::getExchange, dto.getSecurityExchange());
        }
        if (StringUtils.isNotBlank(dto.getStockBoard())) {
            queryWrapper.eq(Security::getBoard, dto.getStockBoard());
        }
        return securityMapper.selectList(queryWrapper);
    }

    @Override
    public void syncList(SyncSecurityListDto dto) {
        List<String> nodes = dto.getNodes();
        for (String node : nodes) {
            syncListByNode(node);
        }
    }

    /**
     * 同步单个证券
     * @param symbol 证券代码
     */
    @Override
    public void sync(String symbol) {
        log.info("开始同步单个证券，证券代码：{}", symbol);
        
        // 使用craw方法抓取单个证券详情
        SecurityResponse securityResponse = crawService.craw(symbol);
        if (securityResponse == null) {
            log.error("抓取单个证券详情失败，证券代码：{}", symbol);
            return;
        }
        Security security = SecurityConvert.INSTANCE.toSecurity(securityResponse);
        securityMapper.update(security, new LambdaQueryWrapper<Security>().eq(Security::getSymbol, symbol));
        log.info("单个证券同步完成，证券代码：{}", symbol);
    }

    public void syncListByNode(String node) {
        List<SecurityResponse> securityResponseList = crawService.crawList(node);
        List<Security> securityList = SecurityConvert.INSTANCE.toSecurityList(securityResponseList);
        securityList.forEach(security -> {
            security.setType(SecurityNodeEnum.getType(node));
            security.setExchange(security.getSymbol().substring(0, 2));
            if (SecurityTypeEnum.STOCK.getCode().equals(security.getType())) {
                security.setBoard(StockBoardCodePrefixEnum.getKeyByStockCode(security.getCode()));
            }
        });
        securityMapper.insertBatch(securityList);
    }

}
